<template>
  <div class="policy">
    <el-card class="box-card" style="margin-top: 35px;border-radius: 20px;">
      <div class="policy-header">
        <div  style="display: flex;align-items: center">
          <h2>用户列表</h2>
          <!-- <el-button @click="add" icon="el-icon-plus" style="margin-left: 20px;background: #699FF3;color: #fff;border-radius: 10px"></el-button> -->
        </div>

        <div class="search-box" style="display: flex;align-items: center">
          <el-form :inline="true" class="demo-form-inline" style="display: flex;align-items: center">
            <el-form-item style="font-size:12px;margin-right:30px;margin-bottom: 0">
              <el-input v-model="form.name" placeholder="请输入用户名称" clearable @clear="getList" prefix-icon="el-icon-search"/>
            </el-form-item>
            <el-form-item style="display: flex;align-items: center;margin-bottom: 0">
              <el-button style="background: #699FF3;color: #fff;border-radius: 24px;margin-bottom: 0" @click="search">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!--       列表-->
      <div class="list">
        <el-table
          :data="tableData"
          style="width: 100%"
          :header-cell-style="{background:'#F4F4F4',border:'none'}"
        >
          <el-table-column
            prop="id"
            label="用户 ID"
            align="center"
          />
          <el-table-column
            prop="name"
            label="用户名称"
            align="center"
          />
          <el-table-column label="用户头像" align="center">
            <template slot-scope="scope">
              <el-avatar size="large" :src="scope.row.avatar"></el-avatar>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="phone"
            label="用户手机号"
            align="center"
          /> -->
          <el-table-column
            prop="role_name"
            label="用户角色"
            align="center"
          />
          <el-table-column
            prop="express_order_count"
            label="下单次数"
            align="center"
          />
          <el-table-column
            prop="balance"
            label="账户余额"
            align="center"
          />
          <el-table-column label="用户状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="available"
                inactive-value="forbidden"
                @change="switchFn(scope.row)">
              </el-switch>
              <!-- <span v-if="scope.row.status=='available'">开启</span>
              <span v-if="scope.row.status=='forbidden'">禁用</span> -->
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" icon="el-icon-edit" style="color: #409EFF;" @click="yeBtn(scope.row)">余额充减</el-button>
              <el-button type="text" size="small" icon="el-icon-edit" style="color: #409EFF;" @click="infoBtn(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paging-device">
        <Pagingdevice
          ref="paging"
          :total-count="totalCount"
          :current_page="current_page"
          :per_page="per_page"
          @select="select"
          @changep="changep"
        />
      </div>
    </el-card>

    <el-dialog
      title="用户"
      :visible.sync="dialogVisible"
      width="800px">
      <el-form ref="form" :model="dataInfo" label-width="80px">
        <el-form-item label="用户名称">
          <el-input v-model="dataInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号">
          <el-input v-model="dataInfo.phone"></el-input>
        </el-form-item>
        <!-- <el-form-item label="用户状态">
          <el-select v-model="dataInfo.status" placeholder="请选择用户状态">
            <el-option label="开启" value="available"></el-option>
            <el-option label="禁用" value="forbidden"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="余额">
          <el-input v-model="dataInfo.balance"></el-input>
        </el-form-item> -->
        <el-form-item label="收费类型:" >
          <el-select v-model="dataInfo.charge_type" placeholder="选择收费类型" clearable>
            <el-option label="未设定，按角色收费表的规则收费" value="none"></el-option>
            <el-option label="增加费用" value="positive"></el-option>
            <el-option label="减少费用" value="negative"></el-option>
            <el-option label="原价" value="original"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" v-if="dataInfo.charge_type=='none'">
          <el-select v-model="dataInfo.role" placeholder="请选择角色">
            <el-option v-for="(item,index) in classList" :label="item.role_name" :value="item.role"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="续重收费规则" v-if="dataInfo.charge_type!='none'">
          <el-table
            :data="dataInfo.charge_weight_amount"
            style="width: 100%">
            <el-table-column label="最大重量" align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.max_weight" autocomplete="off" placeholder="最大重量"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="起始重量" align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.start_weight" autocomplete="off" placeholder="起始重量"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="递增费用(元)" align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.incremental_cost " autocomplete="off" placeholder="递增费用(元)"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="text" size="small" icon="el-icon-delete" style="color: #F56C6C;" @click="del(scope)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-button type="primary" style="width: 100%;" @click="add">新增</el-button>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible=false">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog
      title="余额充减"
      :visible.sync="dialogVisibleYE"
      width="800px">
      <el-form ref="form" :model="balanceInfo" label-width="80px">
        <el-form-item label="用户余额">
          {{ balanceInfo.balance }}
        </el-form-item>
        <el-form-item label="用户状态">
          <el-radio-group v-model="balanceInfo.type">
            <el-radio label="increase">增加</el-radio>
            <el-radio label="decrease">减少</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额">
          <el-input v-model="balanceInfo.number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleYE=false">取 消</el-button>
          <el-button type="primary" @click="balanceSubmit">确 定</el-button>
        </div>
    </el-dialog>

  </div>
</template>

<script>
import {imgURL,userList,userInfo,userEdit,userRoleList,userStatus,userBalance} from "@/api"

import Pagingdevice from '@/components/Pagingdevice'
export default {
  name: "musicList",
  components: {
    Pagingdevice,
  },
  data() {
    return {
      state: 'add',
      item:{},
      isShow: false,
      imgURL,
      form:{
        name:''
      },
      balanceInfo:{
        id:'',
        type:'increase',
        number:'',
        balance:''
      },
      dialogVisible:false,
      dialogVisibleYE:false,
      dataInfo:{},
      tableData:[],
      totalCount: 0,
      current_page: 1,
      per_page:10,
    };
  },
  mounted() {
    this.getList()
  },
  methods: {
    switchFn(row){
      console.log(row.status)
      // return
      userStatus({
        id:row.id,
        status:row.status
      }).then(res=>{
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getList()
        }
      })
    },
    // 列表
    async getList(){
      console.log('列表')
      this.form.page = this.current_page
      await userList(this.form).then(res=>{
        console.log(res)
        if (res.code==10000){
          this.tableData = res.data.list;
          this.totalCount=res.data.meta.total
        }
      })
      await userRoleList({}).then(res=>{
        if (res.code==10000){
          this.classList = res.data;
        }
      })
    },
    onSubmit(){
      userEdit(this.dataInfo).then(res=>{
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.dialogVisible = false
          this.dataInfo = {}
        }
      })
    },
    del(row){
      console.log(row.$index)
      this.dataInfo.charge_weight_amount.splice(row.$index,1)
    },
    add(){
      this.dataInfo.charge_weight_amount.push({
        max_weight:'',
        start_weight:'',
        incremental_cost:''
      })
    },
    // 搜索
    search(){
      this.current_page=1
      this.getList()
    },
    infoBtn(row){
      userInfo({
        id:row.id
      }).then(res=>{
        console.log(res)
        this.dialogVisible = true
        this.dataInfo = res.data
      })
    },
    balanceSubmit(){
      userBalance(this.balanceInfo).then(res=>{
        if (res.code==10000){
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.dialogVisibleYE = false
          this.getList()
          this.balanceInfo={
            id:'',
            type:'increase',
            number:'',
            balance:''
          }
        }
      })
    },
    yeBtn(row){
      this.dialogVisibleYE = true
      this.balanceInfo.id = row.id
      this.balanceInfo.balance = row.balance
    },
    select(num,data){
      this.current_page = num;
      this.per_page = data;
      this.getList()
    },
    changep(num){
      this.current_page = num;
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.policy {
  font-size: 14px;
  padding: 0 80px;
  box-sizing: border-box;
  color: #000;
  &-header {
    margin: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .class-box {

      //float: right;
      margin-bottom: 5px;
    }
    .add-btn {
      width: 100px;
      height: 40px;
      border-radius: 5px;
      background: #1184e6;
      color: #ffffff;
      text-align: center;
      line-height: 40px;
    }
    .add-btn {
      margin-left: 80px;
    }
  }
  .list {
    margin: 30px;
    text-align: center;
  }
  .paging-device {
    margin: 30px;

  }
}

</style>
<style>
.el-table__header-wrapper{
  border-radius: 10px;
}
</style>

